package com.woniuxy.woniubook.uitl;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.util.DigestUtils;

import java.util.Scanner;

public class JSONUtils {
    private static ObjectMapper objectMapper = new ObjectMapper();

    /**
     * 对象转成JSON
     * @param obj
     * @return
     */
    public static String createJson(Object obj){
        try {
            String json = objectMapper.writeValueAsString(obj);
            return json ;
        }catch (Exception e){
            System.out.println("对象转成JSON失败!");
            e.printStackTrace();
        }
        return "" ;
    }


    public static <T> T parseJson(String json ,Class<T> zclass){
        try {
            T t = objectMapper.readValue(json, zclass);
            return t ;
        }catch (Exception e){
            System.out.println("JSON转成对象失败!");
            e.printStackTrace();
        }
        return null;
    }

    public static void main(String[] args) {
        String password = "123456";
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        System.out.println(password);
        Scanner sc = new Scanner(System.in);
        System.out.println("情书密码 :");
        String next = sc.next();
        String s = DigestUtils.md5DigestAsHex(next.getBytes());

        if (s.equals(password)) {
            System.out.println("OK");
        }else {
            System.out.println("No OK");
        }
    }

}
